import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class WechatMediaService extends BaseService<any> {
  private readonly logger = new Logger(WechatMediaService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getMediaPage
   * 对应 PHP: WechatMediaService_admin::getMediaPage()
   * 逻辑类型: undefined - undefined
   */
  async getMediaPage(where: any[]) {
    // 基于PHP真实逻辑: getMediaPage
    // PHP原文: $search_model = $this->model->where([ [ 'site_id', '=', $this->site_id ] ])->withSearch([ 'create_time', 'type' ], $where)->field('*')->append([ 'type...
const search_model = this.model.where([ [ 'site_id', '=', this.site_id ) )).withSearch([ 'create_time', 'type' ), where).field('*').append([ 'type_name' )).order('create_time desc'];
        return this.pageQuery(search_model);
    }

    /**
     * 上传图片素材
     * @return * @throws \EasyWeChat\Kernel\BusinessExceptions\InvalidArgumentBusinessException
     * @throws \Symfony\Contracts\HttpClient\BusinessException\TransportBusinessExceptionInterface
     */
    async addImageMedia(data) {
        const dir = this.root_path + '/' + 'image' + '/'.this.site_id + '/'.date('Ym') + '/'.date('d'];
        const core_upload_service = this.coreUploadService;
        const upload_res = core_upload_service.image(data.file, this.site_id, dir, storage_type: StorageDict.LOCAL];

        const data = [
            site_id: this.site_id,
            type: WechatMediaDict.IMAGE,
            file_path: upload_res.url
        ];
        return this.coreWechatMediaService.addMedia(data);
    }

    /**
     * 上传视频资源
     * @return * @throws \EasyWeChat\Kernel\BusinessExceptions\InvalidArgumentBusinessException
     * @throws \Symfony\Contracts\HttpClient\BusinessException\TransportBusinessExceptionInterface
     */
    async addVideoMedia(data) {
        const dir = this.root_path + '/' + 'video' + '/'.this.site_id + '/'.date('Ym') + '/'.date('d'];
        const core_upload_service = this.coreUploadService;
        const upload_res = core_upload_service.video(data.file, this.site_id, dir, 0, storage_type: StorageDict.LOCAL];

        const data = [
            site_id: this.site_id,
            type: WechatMediaDict.VIDEO,
            file_path: upload_res.url
        ];
        return this.coreWechatMediaService.addMedia(data);
    }

    /**
     * 同步草稿箱
     * @param int pages
     * @return true
     * @throws \EasyWeChat\Kernel\BusinessExceptions\InvalidArgumentBusinessException
     * @throws \Symfony\Contracts\HttpClient\BusinessException\TransportBusinessExceptionInterface
     */
    async syncNewsMedia(int pages = 0) {
        const data = [
            site_id: this.site_id,
            pages: pages
        ];
        const res = this.coreWechatMediaService.syncNewsMedia(data);
        if (pages < res.total_pages) {
            pages++;
            this.syncNewsMedia(pages);
  }

  /**
   * addImageMedia
   * 对应 PHP: WechatMediaService_admin::addImageMedia()
   * 逻辑类型: undefined - undefined
   */
  async addImageMedia(data: any[]) {
    // 基于PHP真实逻辑: addImageMedia
    // PHP原文: $dir = $this->root_path.'/'.'image'.'/'.$this->site_id.'/'.date('Ym').'/'.date('d');         $core_upload_service = new CoreUploadService();         $...
const dir = this.root_path + '/' + 'image' + '/'.this.site_id + '/'.date('Ym') + '/'.date('d'];
        const core_upload_service = this.coreUploadService;
        const upload_res = core_upload_service.image(data.file, this.site_id, dir, storage_type: StorageDict.LOCAL];

        const data = [
            site_id: this.site_id,
            type: WechatMediaDict.IMAGE,
            file_path: upload_res.url
        ];
        return this.coreWechatMediaService.addMedia(data);
    }

    /**
     * 上传视频资源
     * @return * @throws \EasyWeChat\Kernel\BusinessExceptions\InvalidArgumentBusinessException
     * @throws \Symfony\Contracts\HttpClient\BusinessException\TransportBusinessExceptionInterface
     */
    async addVideoMedia(data) {
        const dir = this.root_path + '/' + 'video' + '/'.this.site_id + '/'.date('Ym') + '/'.date('d'];
        const core_upload_service = this.coreUploadService;
        const upload_res = core_upload_service.video(data.file, this.site_id, dir, 0, storage_type: StorageDict.LOCAL];

        const data = [
            site_id: this.site_id,
            type: WechatMediaDict.VIDEO,
            file_path: upload_res.url
        ];
        return this.coreWechatMediaService.addMedia(data);
    }

    /**
     * 同步草稿箱
     * @param int pages
     * @return true
     * @throws \EasyWeChat\Kernel\BusinessExceptions\InvalidArgumentBusinessException
     * @throws \Symfony\Contracts\HttpClient\BusinessException\TransportBusinessExceptionInterface
     */
    async syncNewsMedia(int pages = 0) {
        const data = [
            site_id: this.site_id,
            pages: pages
        ];
        const res = this.coreWechatMediaService.syncNewsMedia(data);
        if (pages < res.total_pages) {
            pages++;
            this.syncNewsMedia(pages);
  }

  /**
   * addVideoMedia
   * 对应 PHP: WechatMediaService_admin::addVideoMedia()
   * 逻辑类型: undefined - undefined
   */
  async addVideoMedia(data: any[]) {
    // 基于PHP真实逻辑: addVideoMedia
    // PHP原文: $dir = $this->root_path.'/'.'video'.'/'.$this->site_id.'/'.date('Ym').'/'.date('d');         $core_upload_service = new CoreUploadService();         $...
const dir = this.root_path + '/' + 'video' + '/'.this.site_id + '/'.date('Ym') + '/'.date('d'];
        const core_upload_service = this.coreUploadService;
        const upload_res = core_upload_service.video(data.file, this.site_id, dir, 0, storage_type: StorageDict.LOCAL];

        const data = [
            site_id: this.site_id,
            type: WechatMediaDict.VIDEO,
            file_path: upload_res.url
        ];
        return this.coreWechatMediaService.addMedia(data);
    }

    /**
     * 同步草稿箱
     * @param int pages
     * @return true
     * @throws \EasyWeChat\Kernel\BusinessExceptions\InvalidArgumentBusinessException
     * @throws \Symfony\Contracts\HttpClient\BusinessException\TransportBusinessExceptionInterface
     */
    async syncNewsMedia(int pages = 0) {
        const data = [
            site_id: this.site_id,
            pages: pages
        ];
        const res = this.coreWechatMediaService.syncNewsMedia(data);
        if (pages < res.total_pages) {
            pages++;
            this.syncNewsMedia(pages);
  }

  /**
   * syncNewsMedia
   * 对应 PHP: WechatMediaService_admin::syncNewsMedia()
   * 逻辑类型: undefined - undefined
   */
  async syncNewsMedia(pages: number) {
    // 基于PHP真实逻辑: syncNewsMedia
    // PHP原文: $data = [             'site_id' => $this->site_id,             'pages' => $pages         ];         $res = (new CoreWechatMediaService())->syncNewsMed...
const data = [
            site_id: this.site_id,
            pages: pages
        ];
        const res = this.coreWechatMediaService.syncNewsMedia(data);
        if (pages < res.total_pages) {
            pages++;
            this.syncNewsMedia(pages);
  }
}
